package yaq.mini.weibo.leafwb.service;

import org.springframework.stereotype.Service;
import yaq.mini.weibo.leafwb.mapper.UserMapper;
import yaq.mini.weibo.leafwb.pojo.User;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserService {
    @Resource
    private UserMapper userMapper;

    public boolean isUserExit(String userId, String password) {
        //先判断是否存在，而不去获取完整的用户信息，以加快检索速度
        User user = null;
        try {
            user = userMapper.getUserByLogin(userId, password);
            if (user != null)
                return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }

    public User getUserByLogin(String userId) {
        User user = null;
        try {
            user = userMapper.getUserByID(userId);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return user;
    }

    public List<User> getUserByIDs(List<String> userIdList) {
        List<User> userList = new ArrayList<>();
        if (userIdList == null)
            return userList;
        try {
            for (String userId : userIdList) {
                User user = userMapper.getUserByID(userId);
                System.out.println(user.getName());
                userList.add(user);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return userList;
        }
        return userList;
    }

    public List<User> listUserExceptRoot() {
        List<User> userList = new ArrayList<>();
        try {
            userList = userMapper.listUserExceptRoot();
        } catch (Exception e) {
            e.printStackTrace();
            return userList;
        }
        return userList;
    }

    public boolean insertUserByRegister(User user) {
        int isSuccess;
        try {
            isSuccess = userMapper.insert(user);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return isSuccess == 1;
    }

    public boolean updateHeadUrl(String userId, String url) {
        try {
            userMapper.updateByHeadUrl(userId, url);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public String getHeadUrl(String userId) {
        String oldUrl = null;
        try {
            oldUrl = userMapper.getHeadUrlById(userId);
        } catch (Exception e) {
            e.printStackTrace();
            return oldUrl;
        }
        return oldUrl;
    }

    public boolean isAllowShare(String userId) {
        boolean isAllowed = true;
        try {
            String str = userMapper.isAllowShare(userId);
            isAllowed = (str.equalsIgnoreCase("T") ? true : false);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return isAllowed;
    }

    public boolean updateAllowShare(String userId,String isAllowShare){
        try{
            userMapper.updateAllowShare(userId,isAllowShare);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean updateUserMessage(String userId, String name, String address, String sex, String birthday, String introduction) {
        try {
            userMapper.updateUserMesById(userId, name, address, sex, birthday, introduction);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean updatePassword(String userId,String newPasswd){
        try{
            userMapper.updatePassword(userId,newPasswd);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
